package com.iMusic.presentationLayer;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.iMusic.businessLogicLayer.Account;
import com.iMusic.businessLogicLayer.Cart;
import com.iMusic.businessLogicLayer.Staff;
import com.iMusic.util.UserMessages;

public class Logout extends HttpServlet {

	/*
	 * Logout
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("Account");
		Staff staff = (Staff) session.getAttribute("Staff");
		Cart cart = (Cart) session.getAttribute("Cart");

		if (account != null) {
			account.logout();
			account = null;
		}
		else if (staff != null) {
			staff.logout();
			staff = null;
		}
		
		session.removeAttribute("Account");
		session.removeAttribute("Staff");
		if (cart != null) cart.removeAccount();
		session.removeAttribute("Cart");
		
		String redirection = "default.jsp";
		
		response.sendRedirect(redirection);
		
	}
}
